#include <iostream>
#include <cstring>
using namespace std;
bool f(int y);
struct jia{
	int m;
	int d;
	jia(int m,int d):m(m),d(d){}
	jia(){}
};
bool c[13][32];
int main(){
	struct jia b[12];
	int a[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31};
	memset(c,false,sizeof(c));
	b[0] = jia(1,1);
	b[1] = jia(5,1);
	b[2] = jia(10,1);
	b[3] = jia(10,2);
	b[4] = jia(10,3);
	b[5] = jia(12,25);
	int y,m,d,s,day = 1;
	cin>>y;
	if(f(y))
		a[2] = 29;
	cin>>m>>d;
	b[6] = jia(m,d);
	if(++d>a[m]){
		d-=a[m];
		m++;
	}
	b[7] = jia(m,d);
	if(++d>a[m]){
		d-=a[m];
		m++;
	}
	b[8] = jia(m,d);
	for(int i = 0;i<3;++i){
		cin>>m>>d;
		b[9+i] = jia(m,d);
	}
	for(int i = 0;i<12;++i)
		c[b[i].m][b[i].d] = true;
	cin>>s;
	d = 1;
	m = 1;
	s = (s+1)%7;
	while(++d){
		if(d>a[m]){
			d = 1;
			m++;
		}
		if(s==0||s==6||c[m][d]){
			day++;
		}
		s = (s+1)%7;
		if(m>12)
			break;
	}
	cout<<day;
	return 0;
}
bool f(int y){
	if(y%400==0)
		return true;
	if(y%4==0&&y%100)
		return true;
	return false;
}